/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//读取表情数据的工具类
import UIKit

//每页显示20个表情
let NumberOfPage = 20

class LSEmoticonTools: NSObject {
    
    //单例全局访问点
    static let sharedTools: LSEmoticonTools = LSEmoticonTools()
    
    //构造函数私有化
    private override init() {
        super.init()
        
        sectionEmoticonsWidthEmoticonArray(defaultEmoticonArray)
        
    }
    
    //创建bundle对象
    private lazy var emoticonBundlePath: String = {
        //emoticonbundle路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
        
        return path
    }()
    
    //MARK: - 读取默认表情图片数据
    private lazy var defaultEmoticonArray: [LSEmoticon] = {
        
        return self.loadEmoticonsWithSubPath("default")
        
    }()
    
    //MARK: - 读取emoji表情图片数据
    private lazy var emojiEmoticonArray: [LSEmoticon] = {
        
        return self.loadEmoticonsWithSubPath("emoji")
        
    }()
    
    //MARK: - 读取浪小花表情图片数据
    private lazy var lxhEmoticonArray: [LSEmoticon] = {
        
        return self.loadEmoticonsWithSubPath("lxh")
        
    }()
    
    //MARK: - 给表情视图(collectionView)提供数据
    lazy var allEmoticonArray: [[[LSEmoticon]]] = {
        return [
            //默认这组数据
            self.sectionEmoticonsWidthEmoticonArray(self.defaultEmoticonArray),
            //emoji这组数据
            self.sectionEmoticonsWidthEmoticonArray(self.emojiEmoticonArray),
            //lxh这组数据
            self.sectionEmoticonsWidthEmoticonArray(self.lxhEmoticonArray)
        ]
    }()
    
    //MARK: - 根据表情数组分成二位数据,表示每种表情分成多少页来显示
    private func sectionEmoticonsWidthEmoticonArray(emoticonArray: [LSEmoticon]) -> [[LSEmoticon]] {
        //计算每种表情分成多少也来显示
        let pageCount = (emoticonArray.count - 1) / NumberOfPage + 1
        
        var tempArray = [[LSEmoticon]]()
        //遍历页数
        for page in 0..<pageCount {
            //从哪个索引开始
            let loc = page * NumberOfPage
            //截取的长度
            var length = NumberOfPage
            
            //表示数组不够截取
            if loc + length > emoticonArray.count {
                //当前数组的个数-开始的截取的索引 = 剩余个数
                length = emoticonArray.count - loc
            }
            //截取范围
            let range = NSMakeRange(loc, length)
            
            
            //截取到子数组
            let subArray = (emoticonArray as NSArray).subarrayWithRange(range)
            
            //添加元素
            tempArray.append(subArray as! [LSEmoticon])
        }
        return tempArray
    }
    
    //MARK: - 根据表情子路径读取相应的数据
    private func loadEmoticonsWithSubPath(subPath: String) -> [LSEmoticon] {
        //获取info.plist文件路径
        let path = self.emoticonBundlePath + "/Contents/Resources/\(subPath)/info.plist"
        print(path)
        //读取指定文件的数据
        let array = NSArray(contentsOfFile: path)!
        var tempArray = [LSEmoticon]()
        
        //遍历数组完成字典转模型
        for dic in array {
            //字典转模型
            let emoticon = LSEmoticon(dic: dic as! [String: AnyObject])
            
            //只有图片才需要拼接路径
            if emoticon.type == "0" {
                let result = (path as NSString).stringByDeletingLastPathComponent
                
                emoticon.path = result + "/" + emoticon.png!
            }
            
            tempArray.append(emoticon)
        }
        return tempArray
    }
    
    //MARK: - 根据表情描述查询表情模型
    func searchEmoticonWithChs(chs: String) -> LSEmoticon? {
        
        //  $0表示闭包中的第一个参数, $1表示第二个参数....
        //  如果只有一行代码还可以省略return  ,以上代码需要看懂
        if let defaultEmoticon = defaultEmoticonArray.filter({ $0.chs == chs}).first {
            return defaultEmoticon
        }
        if let lxhEmoticon = lxhEmoticonArray.filter({ $0.chs == chs}).first {
            return lxhEmoticon
        }
        
        
//        let defaultEmoticon = defaultEmoticonArray.filter { (emoticon) -> Bool in
//            return emoticon.chs == chs
//        }.first
//
//        let lxhEmoticon = lxhEmoticonArray.filter { (emoticon) -> Bool in
//            return emoticon.chs == chs
//        }.first
//
//        if defaultEmoticon != nil {
//            return defaultEmoticon
//        }
//        
//        if lxhEmoticon != nil {
//            return lxhEmoticon
//        }
        
        
        
        
//        //默认表情数组里面查找
//        for value in defaultEmoticonArray {
//            //找到对应的表情模型
//            if value.chs == chs {
//                return value
//            }
//        }
//        for value in lxhEmoticonArray {
//            //找到对应的表情类型
//            if value.chs == chs {
//                return value
//            }
//        }
        return nil
    }
}












